//
//  FXEmoticonKeyboard.swift
//  微博(Swift)
//
//  Created by V on 16/1/12.
//  Copyright © 2016年 V. All rights reserved.
//

import UIKit

class FXEmoticonKeyboard: UIView {
    // MARK: - 初始化
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    // MARK: - 懒加载
    private lazy var toolBar: FXEmoticonToolBar = FXEmoticonToolBar(frame: CGRectZero)
    private lazy var emoticonCollectionView: FXEmoticonCollectionView = {
        let emoticonCollectionView = FXEmoticonCollectionView()
        // emoticonCollectionView.backgroundColor = UIColor.redColor()
        return emoticonCollectionView
    }()
    // pageControl
    private lazy var pageControl: UIPageControl = {
        let pageControl = UIPageControl()
        pageControl.numberOfPages = 5
        pageControl.setValue(UIImage(named: "compose_keyboard_dot_selected"), forKey: "_currentPageImage")
        pageControl.setValue(UIImage(named: "compose_keyboard_dot_normal"), forKey: "_pageImage")
        return pageControl
    }()
    // 
    private lazy var recentLabel: UILabel = {
        let label = UILabel(textColor: UIColor.grayColor(), fontSize: 12)
        label.text = "最近使用的表情"
        return label
    }()
    
    
    // MARK: - 设置UI
    private func setupUI(){
        
        recentLabel.hidden = true
        
        backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        addSubview(toolBar)
        addSubview(emoticonCollectionView)
        addSubview(pageControl)
        addSubview(recentLabel)
        

        // 约束
        toolBar.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self)
            make.width.equalTo(self)
            make.bottom.equalTo(self)
        }
        emoticonCollectionView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self)
            make.bottom.equalTo(toolBar.snp_top)
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
        }
        pageControl.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(toolBar.snp_top)
            make.centerX.equalTo(self)
        }
        recentLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.bottom.equalTo(self).offset(-5-44)
        }
        
        
        
        
        // 点击 toolBar 实现的闭包
        toolBar.childButtonClickClose = {(type: FXEmoticonType) -> () in
            let indexPath: NSIndexPath = NSIndexPath(forItem: 0, inSection: type.rawValue - 1000)
            // 这里选择 left 和 right 都可以实现目标效果，而且效果看不出差别。但是选择 top 就没有切换的效果
            self.emoticonCollectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Right, animated: false)
            // pageControl
             NSNotificationCenter.defaultCenter().postNotificationName(FXEmoticonButtonSelectedNotification, object: indexPath)
        }
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "selectedPageControl:", name: FXEmoticonButtonSelectedNotification, object: nil)
        /// 主队列异步
        /// 主线程会在执行完其他任务之后再执行我们添加的任务
        dispatch_async(dispatch_get_main_queue()) { () -> Void in
            let indexPath = NSIndexPath(forRow: 0, inSection: 1)
            self.emoticonCollectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: false)
        }
        
    }
    
    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    
    
    func selectedPageControl(notify: NSNotification) {
        let indexPath = notify.object as! NSIndexPath
        // print("--------\(indexPath.section)")
        if indexPath.section == 0 {
            pageControl.hidden = true
            recentLabel.hidden = false
        }else {
            pageControl.hidden = false
            recentLabel.hidden = true
            pageControl.numberOfPages = FXEmoticonTools.sharedTools.allEmoticons[indexPath.section].count
            pageControl.currentPage = indexPath.item
        }
    }
}



// MARK: - collectionView的数据源方法

